import clickhouse_driver

def check_table_exists_and_permissions(host, port, user, password, database, table):
    client = clickhouse_driver.Client(host=host, port=port, user=user, password=password, database=database)
    
    # 检查表是否存在
    try:
        result = client.execute(f"EXISTS TABLE {table}")
        if result[0] == 0:
            print(f"Table '{table}' does not exist in database '{database}'.")
            return False
    except Exception as e:
        print(f"Error checking table existence: {e}")
        return False
    
    # 检查是否有查询权限
    try:
        client.execute(f"SELECT 1 FROM {table} LIMIT 1")
        print(f"User has SELECT permission on table '{table}'.")
        return True
    except Exception as e:
        print(f"User does not have SELECT permission on table '{table}': {e}")
        return False

if __name__ == "__main__":
    host = "localhost"
    port = 8123
    user = "default"
    password = ""
    database = "default"
    table = "your_table_name"
    
    check_table_exists_and_permissions(host, port, user, password, database, table)